package org.jrill;

import java.util.List;
import org.jrill.persistence.PersistenceManager;

/**
 * Contoroller class of Workflow lificycle.
 * @author K.Waga
 */
public class WorkflowEngine {

    private PersistenceManager persistenceManager;
    /**
     * Returns {@link Workflow} with specified id from persistence.
     * @param id
     * @return
     */
    public Workflow get(Long id) {
        return getPersistantManager().getById(id);
    }

    /**
     * Returns {@link Workflow} with specified id and version from persistence.
     * @param id
     * @param version
     * @return
     */
    public Workflow get(Long id, long version) {
        return getPersistantManager().getByIdWithVersion(id, version);
    }

    /**
     * Persists {@link Workflow}.
     * @param workFlow workflow to persist
     */
    public void save(Workflow workFlow) {
        getPersistantManager().save(workFlow);
    }

    /**
     * Deletes {@link Workflow} from persistence.
     * @param workFlow workflow to delete
     */
    public void delete(Workflow workFlow) {
        getPersistantManager().delete(workFlow);
    }
    /**
     * Creates new workflow with default activities length.
     * @return
     */
    public Workflow createNewWorkflow() {
        return getPersistantManager().newWorkflow();
    }
    /**
     * Creates new workflow with specified activities length.
     * @return
     */
    public Workflow createNewWorkflow(int length) {
        return getPersistantManager().newWorkflow(length);
    }
    /**
     * Finds Workflows which held current activity by specified participant.
     * @param holder
     * @return
     */
    public List<Workflow> findByCurrentActivityHolder(Participant holder) {
        return getPersistantManager().findUnprocessedWorkflowByParticipant(holder);
    }
    /**
     * Finds Workflows which held current activity by specified participant within the range of specified first row and max row size.
     * @param holder current activity holder to search for
     * @return
     */
    public List<Workflow> findByCurrentActivityHolder(Participant holder, int firstResult, int maxResults) {
        return getPersistantManager().findUnprocessedWorkflowByParticipant(holder, firstResult, maxResults);
    }    
    /**
     * Finds workflows which contains specified participant in its activities.
     * @param participant participant to search for
     * @return list of workflow
     */
    public List<Workflow> findByParticipant(Participant participant) {
        return getPersistantManager().findAllWorkflowByParticipant(participant);
    }
    
    /**
     * Finds workflows which contains specified participant in its activities within the range of specified first row and max row size.
     * @param participant participant to search for
     * @return list of workflow
     */
    public List<Workflow> findByParticipant(Participant participant, int firstResult, int maxResults) {
        return getPersistantManager().findAllWorkflowByParticipant(participant, firstResult, maxResults);
    }
    
    /**
     * Creates workflow from specified workflow template
     * @param template {@link Workflow} template.
     * @return created workflow
     */
    public static Workflow createWorkflowFromTemplate(Workflow template) {
        int size = template.size();
        SimpleWorkflow wf = new SimpleWorkflow(size);
        for (int i = 0; i < size; i++) {
            Activity a = template.getActivity(i);
            a.setWorkflow(null); //disconnect from template
            wf.setActivity(i,(Activity) org.apache.commons.lang.SerializationUtils.clone(template.getActivity(i)));
        }
        return wf;
    }
    /**
     * Returns persistence manager
     * @return persistencemanager
     */
    public PersistenceManager getPersistantManager() {
        return persistenceManager;
    }
    /**
     * Sets persistence manager
     * @param persistenceManager
     */
    public void setPersistantManager(PersistenceManager persistenceManager) {
        this.persistenceManager = persistenceManager;
    }

}
